<?php
require_once '../models/Menu.php';
require_once '../utils/Response.php';

class MenuController {
    private $menu;

    public function __construct() {
        $this->menu = new Menu();
    }

    // 获取所有菜单
    public function getAll() {
        try {
            $menuItems = $this->menu->getAll();
            Response::success($menuItems, '菜单获取成功');
        } catch (Exception $e) {
            Response::error('获取菜单失败: ' . $e->getMessage());
        }
    }

    // 按分类获取菜单
    public function getByCategory() {
        try {
            if (!isset($_GET['category'])) {
                Response::error('请提供分类参数');
            }

            $category = $_GET['category'];
            $menuItems = $this->menu->getByCategory($category);
            Response::success($menuItems, '分类菜单获取成功');
        } catch (Exception $e) {
            Response::error('获取分类菜单失败: ' . $e->getMessage());
        }
    }

    // 搜索菜单
    public function search() {
        try {
            if (!isset($_GET['q'])) {
                Response::error('请提供搜索关键词');
            }

            $keyword = $_GET['q'];
            $menuItems = $this->menu->search($keyword);
            Response::success($menuItems, '搜索结果');
        } catch (Exception $e) {
            Response::error('搜索失败: ' . $e->getMessage());
        }
    }

    // 获取分类列表
    public function getCategories() {
        try {
            $categories = $this->menu->getCategories();
            Response::success($categories, '分类列表获取成功');
        } catch (Exception $e) {
            Response::error('获取分类列表失败: ' . $e->getMessage());
        }
    }

    // 获取菜单详情
    public function getDetail() {
        try {
            if (!isset($_GET['id'])) {
                Response::error('请提供菜单ID');
            }

            $id = $_GET['id'];
            $menuItem = $this->menu->getById($id);

            if (!$menuItem) {
                Response::notFound('菜单项不存在');
            }

            Response::success($menuItem, '菜单详情获取成功');
        } catch (Exception $e) {
            Response::error('获取菜单详情失败: ' . $e->getMessage());
        }
    }

    // 创建菜单项 (管理员功能)
    public function create() {
        try {
            $data = json_decode(file_get_contents('php://input'), true);

            if (!$data) {
                Response::error('无效的请求数据');
            }

            $this->menu->name = $data['name'] ?? '';
            $this->menu->name_en = $data['name_en'] ?? '';
            $this->menu->description = $data['description'] ?? '';
            $this->menu->description_en = $data['description_en'] ?? '';
            $this->menu->price = $data['price'] ?? 0;
            $this->menu->category = $data['category'] ?? '';
            $this->menu->image = $data['image'] ?? '';
            $this->menu->available = $data['available'] ?? 1;
            $this->menu->spicy_level = $data['spicy_level'] ?? 0;
            $this->menu->tags = $data['tags'] ?? '';

            if ($this->menu->create()) {
                Response::success(null, '菜单项创建成功');
            } else {
                Response::error('菜单项创建失败');
            }
        } catch (Exception $e) {
            Response::error('创建菜单项失败: ' . $e->getMessage());
        }
    }

    // 更新菜单项 (管理员功能)
    public function update() {
        try {
            $data = json_decode(file_get_contents('php://input'), true);

            if (!$data || !isset($data['id'])) {
                Response::error('无效的请求数据');
            }

            $this->menu->id = $data['id'];
            $this->menu->name = $data['name'] ?? '';
            $this->menu->name_en = $data['name_en'] ?? '';
            $this->menu->description = $data['description'] ?? '';
            $this->menu->description_en = $data['description_en'] ?? '';
            $this->menu->price = $data['price'] ?? 0;
            $this->menu->category = $data['category'] ?? '';
            $this->menu->image = $data['image'] ?? '';
            $this->menu->available = $data['available'] ?? 1;
            $this->menu->spicy_level = $data['spicy_level'] ?? 0;
            $this->menu->tags = $data['tags'] ?? '';

            if ($this->menu->update()) {
                Response::success(null, '菜单项更新成功');
            } else {
                Response::error('菜单项更新失败');
            }
        } catch (Exception $e) {
            Response::error('更新菜单项失败: ' . $e->getMessage());
        }
    }

    // 删除菜单项 (管理员功能)
    public function delete() {
        try {
            $data = json_decode(file_get_contents('php://input'), true);

            if (!$data || !isset($data['id'])) {
                Response::error('无效的请求数据');
            }

            $this->menu->id = $data['id'];

            if ($this->menu->delete()) {
                Response::success(null, '菜单项删除成功');
            } else {
                Response::error('菜单项删除失败');
            }
        } catch (Exception $e) {
            Response::error('删除菜单项失败: ' . $e->getMessage());
        }
    }
}

// 路由处理
$controller = new MenuController();
$method = $_SERVER['REQUEST_METHOD'];

switch ($method) {
    case 'GET':
        if (isset($_GET['id'])) {
            $controller->getDetail();
        } elseif (isset($_GET['category'])) {
            $controller->getByCategory();
        } elseif (isset($_GET['q'])) {
            $controller->search();
        } elseif (isset($_GET['categories'])) {
            $controller->getCategories();
        } else {
            $controller->getAll();
        }
        break;
    
    case 'POST':
        $controller->create();
        break;
    
    case 'PUT':
        $controller->update();
        break;
    
    case 'DELETE':
        $controller->delete();
        break;
    
    default:
        Response::error('不支持的请求方法', 405);
}
?>